﻿/// <reference path="../Turbo/TurboBase.js" />

var $PropertyOptions = TA.Turbo.Common.PropertyOptions = function PropertyOptions()
{
}
$PropertyOptions.prototype.DefaultValue = null;
$PropertyOptions.prototype.Getter = null;
$PropertyOptions.prototype.Setter = null;
$PropertyOptions.prototype.SerializedGetter = null;
$PropertyOptions.prototype.SerializedSetter = null;
$PropertyOptions.prototype.OnChanging = null;
$PropertyOptions.prototype.OnChanged = null;
$PropertyOptions.prototype.GetInitValue = null;
$PropertyOptions.prototype.Serializable = null;

TA.Turbo.Common.PropertyDefinition = function PropertyDefinition(name, shortName, getInitValue, serializable)
{
	this._name = name;
	this._shortName = shortName;
	this._getInitValue = getInitValue;
	this._serializable = serializable;
}
$PropertyDefinition = TA.Turbo.Common.PropertyDefinition;

$PropertyDefinition.prototype._name = "";
$PropertyDefinition.prototype._shortName = "";
$PropertyDefinition.prototype._getInitValue = null;
$PropertyDefinition.prototype._serializable = null;

$PropertyDefinition.Define = function Define(objectType, propertyName, shortName, defaultValue, options)
{
	if (options == null)
		options = new $PropertyOptions();
	var _____a;
	if (_____a)
		options = new $PropertyOptions();


	if (options.Getter == null)
	{
		options.Getter = function Getter()
		{
			return this._properties[propertyName];
		};
	}
	objectType.prototype["get_" + propertyName] = options.Getter;

	if (options.SerializedGetter == null)
	{
		options.SerializedGetter = function SerializedGetter()
		{
			var v = this["get_" + propertyName]();
			if (v.Serialize != null)
			{
				var sb = new StringBuilder();
				var jw = new JsonWriter(sb);

				v.Serialize(jw);

				return sb.toString();
			}
			return v;
		}
	}
	objectType.prototype["get_" + propertyName + "Serialized"] = options.SerializedGetter;

	if (options.Setter == null)
	{
		options.Setter = function Setter(value)
		{
			if (this._properties[propertyName] != value)
			{
				this["_On" + propertyName + "Changing"]({ NewValue: value });
				this._properties[propertyName] = value;
				this["_On" + propertyName + "Changed"]();
			}
		};
	}
	objectType.prototype["set_" + propertyName] = options.Setter;

	if (options.SerializedSetter == null)
	{
		if (options.Serializable != null)
		{
			options.SerializedSetter = function SerializedSetter(value)
			{
				var t = new options.Serializable();
				t.Deserialize(value);

				this["set_" + propertyName](t);
			}
		}
		else
		{
			options.SerializedSetter = function SerializedSetter(value)
			{
				this["set_" + propertyName](value);
			}
		}
	}
	objectType.prototype["set_" + propertyName + "Serialized"] = options.SerializedSetter;

	if (options.OnChanging == null)
	{
		options.OnChanging = function OnChanging(e)
		{
			this.RaiseEvent(propertyName + "Changing", e);
		};
	}
	objectType.prototype["_On" + propertyName + "Changing"] = options.OnChanging;

	if (options.OnChanged == null)
	{
		options.OnChanged = function OnChanged()
		{
			this.RaiseEvent(propertyName + "Changed");
		};
	}
	objectType.prototype["_On" + propertyName + "Changed"] = options.OnChanged;

	if (options.GetInitValue == null)
	{
		var d = defaultValue;
		if (options.Serializable != null)
		{
			options.GetInitValue = function GetInitValue(obj)
			{
				var state = TA.Turbo.Common.GetState(obj._control); // hidden field with state
				if (state[shortName] !== undefined)
				{
					var res = new options.Serializable();
					res.Deserialize(state[shortName]);
					return res;
				}

				return d;
			};
		}
		else
		{
			options.GetInitValue = function GetInitValue(obj)
			{
				var state = TA.Turbo.Common.GetState(obj._control);

				if (state[shortName] !== undefined)
					return state[shortName];


				return d;
			};
		}
	}

	if (objectType.prototype._propertyShortNames == null)
		objectType.prototype._propertyShortNames = new Object();
	if (objectType.prototype._propertyDefinitions == null)
		objectType.prototype._propertyDefinitions = new Object();
	objectType.prototype._propertyShortNames[shortName] = objectType.prototype._propertyDefinitions[propertyName] =
		new TA.Turbo.Common.PropertyDefinition(propertyName, shortName, options.GetInitValue, options.Serializable);
}
